const log4js = require('log4js')
const filename = (process.env.LOG || "log/")
log4js.configure({
    appenders: {
        console: {
            type: 'console',
            level: 'debug'
        },
        info: {
            type: 'dateFile',
            filename: filename+"/ai-access",
            pattern: '_yyyy-MM-dd.log',
            alwaysIncludePattern: true,
            compress: true,
            numBackups: 365
        }
    },
    categories: {
        default: {
            appenders: [
                'info', 'console'
            ],
            level: 'debug'
        }
    }
})

exports.log = log4js.getLogger('mqtt')

exports.logI = (direction, id, action, data, response = false) => {
    // eslint-disable-next-line no-param-reassign
    id = String(id)
    this.log.info(`${direction} ${response ? response === 'null' ? '' : `RESPONSE` : `REQUEST `},    id = ${id},    ACTION = ${action}  |  DATA = ${data}`)
}

exports.logE = (direction, id, action, data, response = false) => {
    // eslint-disable-next-line no-param-reassign
    id = String(id)
    this.log.error(`${direction} ${response ? response === 'null' ? '' : `RESPONSE` : `REQUEST `},    id = ${id},    ACTION = ${action}  |  DATA = ${data}`)
}
